// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   SelectJspBuilder.java

package tool.radmodel;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;

// Referenced classes of package com.qdone.radmodel:
//			CodeBuilder, Project, Entity, Field

public class SelectJspBuilder
	implements CodeBuilder
{

	private Project project;
	private Entity entity;

	public SelectJspBuilder(Project project)
	{
		this.project = project;
		entity = this.project.getEntity();
	}

	private void saveToFile(StringBuffer codeBuffer)
	{
		String dir = project.getJspRootPath();
		File fileDir = new File(dir);
		if (!fileDir.exists())
			try
			{
				FileUtils.forceMkdir(fileDir);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		String fileName = (new StringBuilder(String.valueOf(project.getOutputPath()))).append("jsp").append(File.separator).append(entity.getTableName().toLowerCase()).append(File.separator).append(entity.getEntityBeanName()).append(".html").toString();
		File file = new File(fileName);
		if (file.exists())
			file.delete();
		try
		{
			FileUtils.writeStringToFile(file, codeBuffer.toString(), "utf-8");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public void saveToFile()
	{
		StringBuffer codeBuffer = new StringBuffer();
		String fileHeader = (new StringBuilder(String.valueOf(project.getTemplatePath()))).append("selectHeader.txt").toString();
		String insertHeaderString;
		try
		{
			insertHeaderString = FileUtils.readFileToString(new File(fileHeader));
		}
		catch (IOException e)
		{
			insertHeaderString = "";
		}
		//datagrid��ݼ���
		StringBuffer dataGridArea = new StringBuffer();
		String primaryKey = entity.getPrimaryKeyField().getPropertyName();
		dataGridArea.append("\r\n");
		dataGridArea.append("                      ").append("<th data-options=\"field:'ck',width:150,checkbox:true\">\r\n");
		dataGridArea.append("                      ").append((new StringBuilder("<th data-options=\"field:'")).append(primaryKey).append("',width:150,sortable:true\">").append(primaryKey).append("</th>\r\n").toString());
		int dataCount = 0;
		for (Iterator<?> iterator = entity.getFields().iterator(); iterator.hasNext();)
		{
			Field field = (Field)iterator.next();
			if (++dataCount == entity.getFields().size())
				dataGridArea.append("                      ").append((new StringBuilder("<th data-options=\"field:'")).append(field.getPropertyName()).append("',width:150,sortable:true\">").append(field.getPropertyName()).append("</th>").toString());
			else
				dataGridArea.append("                      ").append((new StringBuilder("<th data-options=\"field:'")).append(field.getPropertyName()).append("',width:150,sortable:true\">").append(field.getPropertyName()).append("</th>\r\n").toString());
		}

		insertHeaderString = insertHeaderString.replaceAll("_primaryKeyField_", primaryKey);
		insertHeaderString = insertHeaderString.replaceAll("_DataGridColnumFields_", dataGridArea.toString());
		//��ѯ��
		StringBuffer searchArea = new StringBuffer();
		searchArea.append("      ").append("<td>").append((new StringBuilder(String.valueOf(primaryKey))).append(":<input id='").append(primaryKey).append("' name='").append(primaryKey).append("' class=\"easyui-textbox\" autocomplete=\"off\" style=\"width: 80px\"/></td>\r\n").toString());
		int searchAreaCount = 0;
		for (Iterator<?> iterator = entity.getFields().iterator(); iterator.hasNext();)
		{
			Field field = (Field)iterator.next();
			if (++searchAreaCount == entity.getFields().size())
				searchArea.append("                      ").append("<td>").append((new StringBuilder(String.valueOf(field.getPropertyName()))).append(":<input id='").append(field.getPropertyName()).append("' name='").append(field.getPropertyName()).append("' class=\"easyui-textbox\" autocomplete=\"off\" style=\"width: 80px\"/></td>").toString());
			else
				searchArea.append("                      ").append("<td>").append((new StringBuilder(String.valueOf(field.getPropertyName()))).append(":<input id='").append(field.getPropertyName()).append("' name='").append(field.getPropertyName()).append("' class=\"easyui-textbox\" autocomplete=\"off\" style=\"width: 80px\"/></td>\r\n").toString());
		}
		insertHeaderString = insertHeaderString.replaceAll("_SearchAreaFileds_", searchArea.toString());
		insertHeaderString = insertHeaderString.replaceAll("_prefix_", project.getPrefix());
		insertHeaderString = insertHeaderString.replaceAll("_EntityBeanName_", entity.getEntityBeanName());
		codeBuffer.append(insertHeaderString);
		String fileFooter = (new StringBuilder(String.valueOf(project.getTemplatePath()))).append("selectFooter.txt").toString();
		String insertFooterString;
		try
		{
			insertFooterString = FileUtils.readFileToString(new File(fileFooter));
		}
		catch (IOException e)
		{
			insertFooterString = "";
		}
		insertFooterString = insertFooterString.replaceAll("_EntityBeanName_", entity.getEntityBeanName());
		//�����ɱ༭�б�
		StringBuffer searchAreaParam = new StringBuffer(1024);
		searchAreaParam.append("         <tr>\r\n");
		searchAreaParam.append("                      ").append((new StringBuilder("<td>")).append(primaryKey).append("</td>\r\n").toString());
		searchAreaParam.append("                            ").append((new StringBuilder("<td><input name='")).append(primaryKey).append("' class=\"easyui-validatebox\" autocomplete=\"off\" readonly=\"true\" /></td>\r\n").toString());
		searchAreaParam.append("                      ").append("</tr>\r\n");
		int searchCount = 0;
		for (Iterator<?> iterator = entity.getFields().iterator(); iterator.hasNext();)
		{
			Field field = (Field)iterator.next();
			if (++searchCount == entity.getFields().size())
			{
				searchAreaParam.append("                      ").append("<tr>\r\n");
				searchAreaParam.append("                      ").append((new StringBuilder("<td>")).append(field.getPropertyName()).append("</td>\r\n").toString());
				searchAreaParam.append("                            ").append((new StringBuilder("<td><input name='")).append(field.getPropertyName()).append("' class=\"easyui-validatebox\" autocomplete=\"off\" required=\"true\" /></td>\r\n").toString());
				searchAreaParam.append("                      ").append("</tr>\r\n");
			} else
			{
				searchAreaParam.append("                      ").append("<tr>\r\n");
				searchAreaParam.append("                      ").append((new StringBuilder("<td>")).append(field.getPropertyName()).append("</td>\r\n").toString());
				searchAreaParam.append("                            ").append((new StringBuilder("<td><input name='")).append(field.getPropertyName()).append("' class=\"easyui-validatebox\" autocomplete=\"off\" required=\"true\" /></td>\r\n").toString());
				searchAreaParam.append("                      ").append("</tr>\r\n");
			}
		}

		insertFooterString = insertFooterString.replaceAll("_SearchMethodParam_", searchAreaParam.toString());
		codeBuffer.append(insertFooterString);
		saveToFile(codeBuffer);
	}
}
